﻿@{
    ViewBag.Title = "WorkAuditSettingForm";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}
<script>
    var Level = request('Level');
    var keyValue = request('keyValue');
    var RowId = request('RowId');
    var Id = "@Guid.NewGuid().ToString()";
    var HandleConditionJson = [], ConditionJson = [], SetInfo = [];
    $(function () {
        initControl();
        GetGrid1();
        GetGrid2();
        GetGrid3();
    })
    //初始化控件
    function initControl() {
        $("#RoleType").ComboBox({
            data: [{ "key": "1", "value": "岗位" }, { "key": "2", "value": "角色" }],
            id: "key",
            text: "value",
            description: "==请选择==",
            height: "150px"
        }).bind("change", function () {
            $("#RoleId").removeAttr("data-value").removeAttr("data-text").find(".ui-select-text").css("color", "#999").text("==请选择==");
            var _type = parseInt($("#RoleType").attr("data-value"));
            var _url = _type == 2 ? "/BaseManage/Role/GetRoleCheckTreeJson" : "/BaseManage/Post/GetTreeJson";
            $("#RoleId").ComboBox({
                url: _url,
                id: "value",
                text: "text",
                height: "150px",
                description: "==请选择==",
            })
        });
        $("#RoleId").ComboBox({
            id: "value",
            text: "text",
            height: "150px",
            description: "==请选择==",
        });
        if (!!keyValue) {
            Data = top.frames["WorkAuditSetting"].$("#gridTable").jqGridRow()[0];
            $(".form").SetWebControls(Data);
            if (Data.HasHandling == "1") HandleConditionJson = JSON.parse(Data.HandleConditionJson);
            if (Data.HasContion == "1") ChangeData(1, Data.ConditionJson);
            if (!!Data.SetInfo) {
                ChangeData(2, Data.SetInfo);
            } 
           
        } else {
            $("#Level").val(Level);
            $("#Id").val(Id);
        }


    }
    //自定义输入设置
    function GetGrid1() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable1");
        $gridTable.jqGrid({
            datatype: "json",
            height: $(window).height() - 100,
            autowidth: true,
            unwritten: false,
            colModel: [
                { label: '主键', name: 'FieldId', hidden: true },
                { label: '参数名称', name: 'FieldName', index: 'FieldName', width: 250, align: 'left', sortable: false },
                { label: '参数类型', name: 'Type', index: 'Type', width: 200, align: 'left', hidden: true },
                { label: '是否必填', name: 'NeedFlag', index: 'NeedFlag', width: 200, align: 'left', hidden: true },
                {
                    label: '参数类型', name: 'TypeName', index: 'TypeName', width: 200, align: 'left', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        switch (rowObject.Type) {
                            case "1": return "选择型";
                            case "2": return "录入型";
                            default: return rowObject.Type;
                        }
                    }
                },
                {
                    label: '是否必填', name: 'NeedFlagMemo', index: 'NeedFlag', width: 200, align: 'left', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        switch (rowObject.NeedFlag) {
                            case "0": return "否";
                            case "1": return "是";
                            default: return rowObject.NeedFlag;
                        }
                    }
                },
            ],
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        
            $("#gridTable1")[0].addJSONData(HandleConditionJson);
         

    }
    //流转条件
    function GetGrid2() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable2");
        $gridTable.jqGrid({
            datatype: "json",
            height: $(window).height() - 100,
            autowidth: true,
            unwritten: false,
            colModel: [
                { label: '主键', name: 'FieldId', hidden: true },
                { label: '排序', name: 'Sort', hidden: true },
                { label: '条件名称', name: 'FieldName', index: 'FieldName', width: 150, align: 'left', sortable: false },
                { label: '字段名称', name: 'FilterName', index: 'FilterName', width: 150, align: 'left', sortable: false },
                { label: '比较', name: 'FilterId', index: 'FilterId', width: 70, align: 'center', center: false },
                { label: '比较值', name: 'FilterValue', index: 'FilterValue', width: 80, align: 'center', sortable: false },
                { label: '比较逻辑', name: 'Loginc', index: 'Loginc', width: 100, align: 'center', sortable: false },
                { label: '逻辑名称', name: 'LogincName', index: 'LogincName', width: 100, align: 'center', sortable: false, hidden: true },
                { label: '跳转级数', name: 'To', index: 'To', width: 100, align: 'center', sortable: false }
            ],
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        
         $("#gridTable2")[0].addJSONData(ConditionJson);

    }
    //节点执行后操作
    function GetGrid3() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable3");
        $gridTable.jqGrid({
            datatype: "json",
            height: $(window).height() - 100,
            autowidth: true,
            unwritten: false,
            colModel: [ 
                { label: 'DataFieldId', name: 'DataFieldId', hidden: true },
                { label: 'FieldId', name: 'FieldId', hidden: true },
                { label: '排序', name: 'Sort', hidden: true },
                { label: '字段名称', name: 'FieldName', index: 'FilterName', width: 150, align: 'left', sortable: false },
                { label: '比较', name: 'FilterId', hidden: true },
                { label: '比较', name: 'FilterName', index: 'FilterName', width: 80, align: 'center', sortable: false },
                { label: '比较值', name: 'FilterValue', index: 'NeedFlag', width: 70, align: 'center', sortable: false },
                { label: '逻辑', name: 'Loginc', index: 'FilterValue', width: 100, align: 'center', sortable: false },
                { label: '逻辑名称', name: 'LogincName', index: 'LogincName', width: 100, align: 'center', sortable: false, hidden: true },
                { label: '数据字段', name: 'DataFieldName', index: 'DataFieldName', width: 150, align: 'left', sortable: false },
                { label: '数据值', name: 'DataFieldValue', index: 'DataFieldValue', width: 100, align: 'center', sortable: false }
            ],
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
         $("#gridTable3")[0].addJSONData(SetInfo);
    }

    //添加自定义输入设置
    function btn_addHandle() {
        dialogOpen({
            id: "SettingDetailForm",
            title: '添加自定义输入设置',
            url: '/SystemManage/Setting/SettingDetailForm?Type=1',
            width: "500px",
            height: "350px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    }
    //编辑自定义输入设置
    function btn_updateHandle() {
        var keyValue = $("#gridTable1").getGridParam('selrow');
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "SettingDetailForm",
                title: '添加自定义输入设置',
                url: '/SystemManage/Setting/SettingDetailForm?Type=1&keyValue=' + keyValue,
                width: "500px",
                height: "350px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }

    }
    //删除自定义输入设置
    function btn_delHandle() {
        var keyValue = $("#gridTable1").getGridParam('selrow');
        if (keyValue) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable1").delRowData(keyValue);
                    dialogMsg("移除成功。", 1);
                }
            });

        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }
    //添加流转条件
    function btn_addCondition() {
        dialogOpen({
            id: "SettingDetailForm",
            title: '添加流转条件',
            url: '/SystemManage/Setting/SettingDetailForm?Type=2',
            width: "500px",
            height: "350px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    }
    //编辑流转条件
    function btn_updateCondition() {
        var keyValue = $("#gridTable2").getGridParam('selrow');
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "SettingDetailForm",
                title: '编辑流转条件',
                url: '/SystemManage/Setting/SettingDetailForm?Type=2&keyValue=' + keyValue,
                width: "500px",
                height: "350px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
    }
    //删除流转条件
    function btn_delCondition() {
        var keyValue = $("#gridTable2").getGridParam('selrow');
        if (keyValue) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable2").delRowData(keyValue);
                    dialogMsg("移除成功。", 1);
                }
            });

        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }
    //添加节点执行后操作
    function btn_addSetInfo() {
        dialogOpen({
            id: "SettingDetailForm",
            title: '添加节点执行后操作',
            url: '/SystemManage/Setting/SettingDetailForm?Type=3',
            width: "500px",
            height: "350px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    }
    //编辑节点执行后操作
    function btn_updateSetInfo() {
        var keyValue = $("#gridTable3").getGridParam('selrow');
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "SettingDetailForm",
                title: '编辑节点执行后操作',
                url: '/SystemManage/Setting/SettingDetailForm?Type=3&keyValue=' + keyValue,
                width: "500px",
                height: "350px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
    }
    //删除节点执行后操作
    function btn_delSetInfo() {
        var keyValue = $("#gridTable3").getGridParam('selrow');
        if (keyValue) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable3").delRowData(keyValue);
                    dialogMsg("移除成功。", 1);
                }
            });

        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }
    //面板切换
    function BaseClick(ss) {
        if (ss == 0) {
            $(".btn-group").attr("hidden", "hidden");
            $("#panel1").removeAttr("hidden");
            $("#panel2").attr("hidden", "hidden")
            $("#panel3").attr("hidden", "hidden")
            $("#panel4").attr("hidden", "hidden")

        }
        if (ss == 1) {
            $("#panel2").removeAttr("hidden");
            $("#panel1").attr("hidden", "hidden")
            $("#panel3").attr("hidden", "hidden")
            $("#panel4").attr("hidden", "hidden")

        }
        if (ss == 2) {
            $("#panel3").removeAttr("hidden");
            $("#panel1").attr("hidden", "hidden")
            $("#panel2").attr("hidden", "hidden")
            $("#panel4").attr("hidden", "hidden")

        }
        if (ss == 3) {
            $("#panel4").removeAttr("hidden");
            $("#panel1").attr("hidden", "hidden")
            $("#panel2").attr("hidden", "hidden")
            $("#panel3").attr("hidden", "hidden")

        }
    }

    //转换显示数据
    function ChangeData(type, data) {
        //流转条件
        if (type == 1) {
            var json = JSON.parse(data);
            for (var i = 0; i < json.length; i++) {
                var setinfo = json[i].SetInfo;
                var set = JSON.parse(setinfo);
                for (var j = 0; j < set.length; j++) {
                    ConditionJson.push({
                        FieldId: json[j].FieldId,
                        Sort: set[j].Sort,
                        FieldName: set[j].FieldName,
                        FilterName: set[j].FilterName,
                        FilterId: set[j].FilterId,
                        FilterValue: set[j].FilterValue,
                        Loginc: set[j].Loginc,
                        LogicName: set[j].LogicName,
                        To: json[i].To,
                    });
                }
            }
        }
        debugger;
        //节点数据处理
        if (type == 2) {
            var DataToSet = JSON.parse(data);
            if (DataToSet.length > 0) {
                var json = JSON.parse(DataToSet[0].DataToSet);
                for (var i = 0; i < json.length; i++) {
                    var setinfo = json[i].SetInfo;
                    var set = JSON.parse(setinfo);
                    for (var j = 0; j < set.length; j++) {
                        SetInfo.push({
                            FieldId: set[j].FieldId,
                            FieldName: set[j].FieldName,
                            Sort: set[j].Sort,
                            FilterId: set[j].FilterId,
                            FilterName: set[j].FilterName, 
                            FilterValue: set[j].FilterValue,
                            Loginc: set[j].Loginc,
                            LogicName: set[j].LogicName,
                            DataFieldId: json[i].DataFieldId,
                            DataFieldName: json[i].DataFieldName,
                            DataFieldValue: json[i].DataFieldValue
                        });
                    }
                }
            }
        }
    }

    function AcceptClick() {
        debugger;
        if (!$('.form').Validform()) {
            return false;
        }
        var postData = $(".form").GetWebControls("");
        postData["RoleTypeName"] = $("#RoleType").attr("data-text");
        postData["RoleName"] = $("#RoleId").attr("data-text");
        //额外操作
        var HandleConditionJson = $("#gridTable1").jqGrid("getRowData");
        postData["HandleConditionJson"] = JSON.stringify(HandleConditionJson);
        //是否有额外操作
        if (HandleConditionJson.length > 0) {
            postData["HasHandling"] = 1;
        } else {
            postData["HasHandling"] = 0;
        }
        //流转条件
        var ConditionJson = $("#gridTable2").jqGrid("getRowData");
        //是否有流转条件限制
        if (ConditionJson.length > 0) {
            postData["HasContion"] = 1;
            var ConditionData = [];
            var SetInfo = [];
            for (var i = 0; i < ConditionJson.length; i++) {
                var FieldId = ConditionJson[i].FieldId;
                var Form = Level;
                var To = ConditionJson[i].To;
                SetInfo.push({
                    FieldId: ConditionJson[i].FieldId,
                    Sort: ConditionJson[i].Sort,
                    FieldName: ConditionJson[i].FieldName,
                    FilterId: ConditionJson[i].FilterId,
                    FilterName: ConditionJson[i].FilterName,
                    FilterValue: ConditionJson[i].FilterValue,
                    Loginc: ConditionJson[i].Loginc,
                    LogicName: ConditionJson[i].LogicName
                })
                if (ConditionJson[i].Loginc == null || ConditionJson[i].Loginc === "") {
                    ConditionData.push({
                        FieldId: FieldId,
                        From: Form,
                        To: To,
                        SetInfo: JSON.stringify(SetInfo)
                    });
                    SetInfo = [];
                }
            }
        } else {
            postData["HasContion"] = 0;
        }
        debugger;
        postData["ConditionJson"] = JSON.stringify(ConditionData);
        //节点执行后操作
        var SteInfo = $("#gridTable3").jqGrid("getRowData");
        postData["SetInfo"] = "";
        debugger;
        if (SteInfo.length > 0) {
            var SteInfoData = [];
            var SetInfo = [];
            var DataToSet = [];
            for (var i = 0; i < SteInfo.length; i++) {
                var DataFieldId = SteInfo[i].DataFieldId;
                var DataFieldName = SteInfo[i].DataFieldName;
                var DataFieldValue = SteInfo[i].DataFieldValue;
                SetInfo.push({
                    FieldId: SteInfo[i].FieldId,
                    Sort: SteInfo[i].Sort,
                    FieldName: SteInfo[i].FieldName,
                    FilterId: SteInfo[i].FilterId,
                    FilterName: SteInfo[i].FilterName,
                    FilterValue: SteInfo[i].FilterValue,
                    Loginc: SteInfo[i].Loginc,
                    LogicName: SteInfo[i].LogicName
                })
                if (SteInfo[i].Loginc == null || SteInfo[i].Loginc === "") {
                    DataToSet.push({
                        DataFieldId:DataFieldId,
                        DataFieldName: DataFieldName,
                        DataFieldValue: DataFieldValue,
                        SetInfo: JSON.stringify(SetInfo)
                    });
                   
                    SetInfo = []; 
                }
            }
            SteInfoData.push({
                RejectToLevel: "",
                DataToSet: JSON.stringify(DataToSet)
            });
            postData["SetInfo"] = JSON.stringify(SteInfoData);
        }
        var $gridTable = top.frames["WorkAuditSetting"].$("#gridTable");
        if (!!keyValue) {
            debugger;
            $gridTable.setRowData(RowId, postData);
        } else {
            $gridTable.addRowData(($gridTable.jqGrid("getRowData").length + 1), postData);
        }
        dialogMsg("操作成功。", 1);
        dialogClose();

    }
</script>

<div class="ui-layout" style="height: 100%; width: 100%;">

    <div class="gridPanel" style="width: 100%; border-top: 1px solid #CCC;">
        <table style="width:100%;">
            <tr style="width:100%;">
                <td style="width: 40%; position: relative; height: 100%; padding-bottom: 20px;" valign="top">
                    <div>
                        <ul class="nav nav-tabs" style="background-color:#f6f8f8;">
                            <li class="active" onclick="BaseClick(0)">
                                <a href="#panel1" data-toggle="tab">基本配置</a>
                            </li>
                            <li onclick="BaseClick(1)">
                                <a href="#panel2" data-toggle="tab">自定义输入设置</a>
                            </li>
                            <li onclick="BaseClick(2)">
                                <a href="#panel3" data-toggle="tab">流转条件</a>
                            </li>
                            <li onclick="BaseClick(3)">
                                <a href="#panel4" data-toggle="tab">节点执行后操作</a>
                            </li>
                        </ul>
                    </div>

                </td>
            </tr>
        </table>
    </div>
    <div class="tab-pane active" id="panel1">
        <table class="form">
            <tr>
                <th class="formTitle">审核名称<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="Id" type="hidden" />
                    <input id="Name" type="text" class="form-control" isvalid="yes" checkexpession="NotNull">
                </td>
            </tr>
            <tr>
                <th class="formTitle">审核级数<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="Level" type="number" class="form-control" isvalid="yes" readonly checkexpession="NotNull">
                </td>
            </tr>
            <tr>
                <th class="formTitle">执行类别<font face="宋体">*</font></th>
                <td class="formValue">
                    <div id="RoleType" type="selectTree" class="ui-select" isvalid="yes" checkexpession="NotNull"></div>
                </td>
            </tr>
            <tr>
                <th class="formTitle">执行角色<font face="宋体">*</font></th>
                <td class="formValue">
                    <div id="RoleId" type="selectTree" class="ui-select" isvalid="yes" checkexpession="NotNull"></div>
                </td>
            </tr>


        </table>
    </div>
    <div class="tab-pane" id="panel2" hidden>
        <div class="btn-group" style="position: absolute; top: 10px; right: 30px;z-index:999; ">
            <a id="lr-addHandle" class="btn btn-default " onclick="btn_addHandle(); "><i class="fa fa-plus"></i>&nbsp;添加</a>
            <a id="lr-editHandle" class="btn btn-default " onclick="btn_updateHandle(); "><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
            <a id="lr-delHandle" class="btn btn-default " onclick="btn_delHandle()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
        </div>
        <div class="gridPanel">
            <table id="gridTable1"></table>
        </div>
    </div>
    <div class="tab-pane " id="panel3" hidden>
        <div class="btn-group" style="position: absolute; top: 10px; right: 30px;z-index:999; ">
            <a id="lr-addCondition" class="btn btn-default " onclick="btn_addCondition(); "><i class="fa fa-plus"></i>&nbsp;添加</a>
            <a id="lr-editCondition" class="btn btn-default " onclick="btn_updateCondition(); "><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
            <a id="lr-delCondition" class="btn btn-default " onclick="btn_delCondition()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
        </div>
        <div class="gridPanel">
            <table id="gridTable2"></table>
        </div>
    </div>
    <div class="tab-pane " id="panel4" hidden>
        <div class="btn-group" style="position: absolute; top: 10px; right: 30px;z-index:999; ">
            <a id="lr-addSetInfo" class="btn btn-default " onclick="btn_addSetInfo(); "><i class="fa fa-plus"></i>&nbsp;添加</a>
            <a id="lr-editSetInfo" class="btn btn-default " onclick="btn_updateSetInfo(); "><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
            <a id="lr-delSetInfo" class="btn btn-default " onclick="btn_delSetInfo()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
        </div>
        <div class="gridPanel">
            <table id="gridTable3"></table>
        </div>
    </div>

</div>


